package com.voice.call.controller;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.voice.call.common.Const;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.utils.StringUtil;
import com.voice.call.version.ApiVersion;


//文件上传
@RestController
@RequestMapping("/api/{version}/upload/")
public class UploadFileController {
	
	private final Logger log = LoggerFactory.getLogger(UploadFileController.class);
	
	@Resource
	private Properties configProperties;
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "file", method = RequestMethod.POST) //produces = Const.JSON_UTF8
	public Result uploadFileList(HttpServletRequest request,@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {
		
		if(file==null || (file!=null && StringUtil.isEmpty(file.getOriginalFilename()))){
			log.error("dmp文件上传"+ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR);
		}
		
		//String imgUrl = configProperties.getProperty(Const.UPLOAD_HTTP)+"dmp/"+file.getOriginalFilename();
		//log.info("dmp文件下载路径:{}",imgUrl);
		String path = configProperties.getProperty(Const.UPLOAD_SAVE_PATH)+"dmp/";
		try {
			
			File targetFile = new File(path, file.getOriginalFilename());
			//判断文件夹是否已经存在，如果已经存在了重新建   
			if (!targetFile.exists()) {   
			    targetFile.mkdirs();   
			}
			//转存文件
			file.transferTo(targetFile);
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("保存文件失败");
			return new Result(ResultEnum.FAILED);
		}
		return new Result();
	}

}
